<?php
namespace im\modules\chat\controllers;

use Yii;
use yii\db\Expression;
use im\controllers\AuthController;
use app\models\Faq;
use common\component\exception\ErrMap;
use common\component\tool\Tool;
use common\models\OperationLog;
use common\behaviors\Sort;
use yii\helpers\Html;

class FaqController extends AuthController
{
    public function behaviors()
    {
        return [
            Sort::className(),

        ];
    }
    /*列表*/
    public function actionFaqList()
    {
        if(isset($this->get['page'])){
            $currPage = intval($this->get['page']?$this->get['page']:1);
        }else{
            $currPage = 1;
        }
        $pageSize = Tool::getPageSize();
        // echo $pageSize;

        $faq = new Faq;
        $count = $faq::find()->where('app_id = :app_id', [':app_id' => $this->app_id])->count();
        $faqList = $faq::find()->select(['id', 'question', 'display', 'click_count'])
                        ->where('app_id = :app_id', [':app_id' => $this->app_id])
                        ->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC])
                        ->offset($pageSize*($currPage-1))->limit($pageSize)
                        ->asArray()->all();
        $res = array(
            'list' => $faqList,
            'pageInfo' => array(
                'count' => intval($count),
                'currPage' => $currPage,
                'pageSize' => $pageSize,
            ),
        );
        return Tool::showRes([0, '获取成功'], $res);
    }

    /*添加*/
    public function actionAddFaq()
    {
        if(Yii::$app->request->isPost){
            $faq = new Faq;
            if($faq->addFaq($this->post, $this->app_id)){
                $backInfo = [0, '添加问答成功'];//返回信息
            }else{
                if($faq->hasErrors()){
                    $backInfo = ErrMap::getError(10005);//返回信息
                }
            }
        };

        if(!isset($backInfo)){
            $backInfo = ErrMap::getError(10005);//返回信息
        }
        if(!isset($exception)){
            $exception = '';//异常信息
        }
        //操作日志
        if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
            return $res;
        };
    }

    /*修改*/
    public function actionModifyFaq()
    {
        /*取出单个数据*/
        if(Yii::$app->request->isGet){
            // P($this->get);
            $id = isset($this->get['id'])?$this->get['id']:'';
            if($id){
                $faq = Faq::find()
                            ->where('id = :id', [':id' => $id])
                            ->asArray()->one();
                // P($faq);
                if(!is_null($faq)){
                    // P($faq);
                    if(!empty($faq['answer'])){
                        $faq['answer'] = Html::decode($faq['answer']);
                    }
                    return Tool::showRes([0, '获取成功'], $faq);
                };
            }
        }

        /*如果有数据，进行修改*/
        if(Yii::$app->request->isPost){
            $id = isset($this->post['Faq']['id'])?intval($this->post['Faq']['id']):'';
            if(!$id){
                $backInfo = ErrMap::getError(10005);//返回信息
            }else{
                $faq = new Faq;
                if($faq->modifyFaq($this->post, $id)){
                    $backInfo = [0, '修改问答成功'];//返回信息
                }else{
                    if($faq->hasErrors()){
                        $backInfo = ErrMap::getError(10005);//返回信息
                        $exception = $faq->getErrors();//异常信息
                    }
                }
            }

            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };

        }

        return Tool::showRes(ErrMap::getError(10005));
    }


    /*删除内容*/
    public function actionDeleteFaq()
    {
        $id = isset($this->get['id'])?$this->get['id']:'';
        if($id){
            $faq = new Faq;
            if($faq->deleteAll('id = :id', [':id' => $id])){//删除成功
                $backInfo = [0, '删除问答成功'];//返回信息
            }else{//删除失败
                $backInfo = ErrMap::getError(10013, '删除问答失败');//返回信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), '') ) != 'boolean'){
                return $res;
            };

        }
        return Tool::showRes(ErrMap::getError(10005));
    }

    /*排序*/
    public function actionSetSort()
    {
        if(Yii::$app->request->isPost){
            $sort = $this->post['sort'];
            // P($sort);
            if(!empty($sort) and is_array($sort)){
                if($this->setSortList(new Faq, $sort)){
                    $backInfo = [0, '设置问答排序成功'];//返回信息
                }
            }
            if(!isset($backInfo)){
                $backInfo = ErrMap::getError(10005);//返回信息
            }
            if(!isset($exception)){
                $exception = '';//异常信息
            }
            //操作日志
            if(gettype( $res = OperationLog::addLog(Tool::operationLogArgs($backInfo, $this->userInfo), $exception) ) != 'boolean'){
                return $res;
            };
        }
        return Tool::showRes(ErrMap::getError(10005));
    }

}
